var pageNum;
var currentTag;
var currentLevel;
var currentType;
var total;
var lastPage;
var async = false;
var videoInfo = new Array();

function retrieveVideo(tags, levels, types, pNum) {
	pageNum = pNum;
	currentTag = tags;
	currentLevel = levels;
	currentType = types;

	clearVideo();

	var url = "/video.php/?function=getVideo";
	if (tags != "")
		url += "&tags=" + tags;
	if (types != "")
		url += "&types=" + types;
	if (levels != "")
		url += "&levels=" + levels;
	url += "&pageNum=" + pageNum;
	async = true;
	$.ajax( {
		url : url,
		success : getVideoInfo
	});
}

function clearVideo() {
	$("#vc").remove();
	videoInfo = new Array();
}

function getVideoInfo(data, status) {
	var dataParts = data.split("&");
	total = dataParts[0];
	lastPage = parseInt(total / 15);
	if (total % 15 == 0)
		lastPage++;
	if (total == 0) {
		async = false;
		$(".videoNavigation").css("display", "none");
		$("#pageInfo").css("display", "none");
	} else {
		$("<div id='vc'></div>").appendTo('#videoList');
		var videos = dataParts[1].split(",");

		updateNavigationBtn(videos.length);

		$
				.each(
						videos,
						function() {
							$
									.ajax( {
										url : 'http://gdata.youtube.com/feeds/api/videos/' + this + '?key=AI39si5mNTMrNzZM997rDSvzZAIzKL9Gqn2VJmlk8Hhm0H3DlKRYvw9UwP3pE5fbev4XEgHusRoYTqFFTcqGkQbfccdEyvAZQQ' 
												+ '&v=2&alt=json-in-script',
										success : showVideos,
										dataType : 'jsonp'
									});
						});
	}
}

function clickFirst() {
	if ($("#first").attr("allow") == "true" && !async)
		retrieveVideo(currentTag, currentLevel, currentType, 0);
}

function clickPrev() {
	if ($("#prev").attr("allow") == "true" && !async) {
		pageNum--;
		retrieveVideo(currentTag, currentLevel, currentType, pageNum);
	}
}

function clickNext() {
	if ($("#next").attr("allow") == "true" && !async) {
		pageNum++;
		retrieveVideo(currentTag, currentLevel, currentType, pageNum);
	}
}

function clickLast() {
	if ($("#last").attr("allow") == "true" && !async) {
		pageNum = lastPage;
		retrieveVideo(currentTag, currentLevel, currentType, pageNum);
	}
}

function showVideos(data, status) {
	createVideoObject(data).appendTo('#vc');
	async = false;

	if (empty) {
		onClick($(".videoRecord")[0]);
		empty = false;
	}
}

function updateNavigationBtn(videoCount) {
	var next = $("#next");
	var last = $("#last");
	if (pageNum != lastPage) {
		next.removeClass("nextGrey").addClass("nextAllow")
				.attr("allow", "true");
		last.removeClass("grey").addClass("allow").attr("allow", "true");
	} else {
		next.removeClass("nextAllow").addClass("nextGrey").attr("allow",
				"false");
		last.removeClass("allow").addClass("grey").attr("allow", "false");
	}

	$("#pageInfo").text(
			(pageNum + 1) + " " + m.page + " (" + m.from + " " + (lastPage + 1)
					+ ")");

	var first = $("#first");
	var prev = $("#prev");
	if (pageNum == 0) {
		first.removeClass("allow").addClass("grey").attr("allow", "false");
		prev.removeClass("prevAllow").addClass("prevGrey").attr("allow",
				"false");
	} else {
		first.removeClass("grey").addClass("allow").attr("allow", "true");
		prev.removeClass("prevGrey").addClass("prevAllow")
				.attr("allow", "true");
	}
}

function createVideoObject(data) {
	var video = data.entry;
	var group = video.media$group;
	var title = group.media$title.$t.split(" - Skipping tricktionary")[0];
	var url = group.yt$videoid.$t;
	videoInfo[url] = video;
	var vObject = $('<div class="videoRecord notSelectedRecord" video="'
			+ url
			+ '"><table cellspacing="0" cellpadding="0"><tr><td><div class="recordImg notSelectedRecordImg"></div></td><td>'
			+ title + '</td></div>');
	if (url == $("#player").attr("currentVideo")) {
		vObject.removeClass("notSelectedRecord").addClass("selectedRecord")
				.attr("selected", "true").find(".recordImg").removeClass(
						"notSelectedRecordImg").addClass("selectedRecordImg");
	}
	vObject.click(function() {
		onClick(this);
	}).hover(function() {
		var selected = $(this).attr("selected");
		if (!selected || selected == 'false')
			$(this).find(".recordImg").addClass("hoverRecordImg");
	}, function() {
		var selected = $(this).attr("selected");
		if (!selected || selected == 'false')
			$(this).find(".recordImg").removeClass("hoverRecordImg");
	});
	return vObject;
}

function onClick(object) {
	$("#showComments").text(m.showComments);
	$("#showComments").attr("checked", "false");
	$("#commentsContainer").remove();
//	replaceVotch();
	var selected = $(".videoRecord[selected='true']");
	if (selected.length != 0) {
		$(selected[0]).attr("selected", "false").removeClass("selectedRecord")
				.addClass("notSelectedRecord");
		$(selected[0]).find(".recordImg").removeClass("selectedRecordImg")
				.find(".recordImg").addClass("notSelectedRecordImg");
	}
	$(object).removeClass("notSelectedRecord").addClass("selectedRecord").attr(
			"selected", "true");
	;
	$(object).find(".recordImg").removeClass("notSelectedRecordImg")
			.removeClass("hoverRecordImg").addClass("selectedRecordImg");

	var url = $(object).attr("video");
	var player = $("#player");
	player.empty();
	$(
			'<object style="height: 350px; width: 425px">'
					+ '<param name="movie" value="http://www.youtube.com/v/'
					+ url
					+ '">'
					+ '<param name="allowFullScreen" value="true">'
					+ '<param name="allowScriptAccess" value="always">'
					+ '<embed src="http://www.youtube.com/v/'
					+ url
					+ '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="350">'
					+ '</object>').appendTo(player);
	player.attr("currentVideo", url);
	if (containsInPlaypilst(url)) {
		$("#addToPlaylist").removeClass("notselected").addClass("selected");
		$("#addToPlaylist").attr("checked", "true")
				.text(m.removeFromPlaylist);
	} else {
		$("#addToPlaylist").removeClass("selected").addClass("notselected");
		$("#addToPlaylist").attr("checked", "false").text(m.addToPlaylist);
	}
	var video = videoInfo[url];
	var commentUrl = video.gd$comments.gd$feedLink.href + "&alt=json-in-script";
	$.ajax( {
		url : commentUrl,
		success : pasteComments,
		dataType : 'jsonp'
	});
}

function pasteComments(data, status) {
	$("<div id='commentsContainer' style='display: none;'></div>").insertAfter(
			"#postComment");
	if (data.feed.openSearch$totalResults.$t != 0) {
		$("#showComments").css("display", "block");
		$("#showComments").removeClass("selected").addClass("notselected");
		$("#showComments").attr("check", false);
		$.each(data.feed.entry, function() {
			var date = this.published.$t.split("T", 2)[0];
			var time = this.published.$t.split("T", 2)[1];
			var day = date.split("-", 3)[2];
			var month = date.split("-", 3)[1];
			var year = date.split("-", 3)[0];
			var d = day + "." + month + "." + year + " " 
					+ time.split(":", 2)[0] + ':' + time.split(":", 2)[1];
			$(
					"<div class='comment'>"
							+ this.content.$t 
							+ "<br/><span class='commentDescr'>"
							+ this.author[0].name.$t + "</span>&nbsp;<span class='commentTime'>"
							+ d + "</span>"
							+ "</div>").appendTo(
					"#commentsContainer");
		});
	} else {
		$("#showComments").css("display", "none");
	}
//	replaceVotch();
}